
#!/usr/bin/perl
use Getopt::Std;
getopt("d",\%args);
my $dir = $args{d};
chomp($dir);
if($dir eq "")
{
        die "entered dir name where subfolders are created is empty\n";
}
open(TEST,">$dir/23/male/TEST");
print "TEST\n";
close(TEST);
system("gunzip $dir/23/male/*.gz");
system("gunzip $dir/23/female/*.gz");
$ku =0;
open BUFF,"<$dir/processed_beagle_input.fam" or die "can't open unprocessed_beagle_input.tfam\n";
while($line = <BUFF>)
{
        chomp($line);
        @samp_tf = split(" ",$line);
        $samp_hash{$samp_tf[1]} = $line;
}
#male start
if ((-e "$dir/23/male/geno_male.bgl")) 
{

	open MBUFF,"<$dir/23/male/beagleout_male.geno_male.bgl.gz.phased" or $ku =2;
	if($ku ==2)
	{
        	print BUF "failed $dir/23/male/beagleout_male.geno_male.bgl.gz.phased\n";
	}
	open(MTFAM,">$dir/23/male/male.tfam");
	open(MTPED,">$dir/23/male/male.tped");
	$male = <MBUFF>;
	@male = split(" ",$male);
	$male = <MBUFF>;
	for($i=2;$i<@male;$i++)
	{
        	chomp($male[$i]);
        	if(exists($samp_hash{$male[$i]}))
        	{
                	print MTFAM "$samp_hash{$male[$i]}\n";
        	}
        	else
        	{
                	die "male tfam sample name not same as all tfam name\n";
        	}
	}
	close(MTFAM);
	open RSID, "$dir/23/male/markers.txt" or die "no file available $dir/23/male/markers.txt\n";
	while($line=<RSID>)
	{
        	chomp($line);
        	@marker = split(" ",$line);
        	$rsid{$marker[0]} = $marker[1];
	}
	close(RSID);


	while($line = <MBUFF>)
	{
        	chomp($line);
        	@male = split(" ",$line);
        	if(exists($rsid{$male[1]}))
        	{
                	$pos = $rsid{$male[1]};
        	}
        	else
		{
                	die "doesn't exist position for $male[1]";
        	}
        	print MTPED "23 $male[1] 0 $rsid{$male[1]} ";
        	for($i=2;$i<@male;$i++)
        	{
                	print MTPED " $male[$i] $male[$i]";
        	}
        	print MTPED "\n";
	}
	close(MBUFF);
	close(MTPED);
}
#female start
if ((-e "$dir/23/female/geno_female.bgl"))
{
	open FBUFF,"<$dir/23/female/beagleout_female.geno_female.bgl.gz.phased" or $ku =1;
	open BUF,">>$dir/failed.txt";
	if($ku ==1)
	{
        	print BUF "failed $dir/23/female/beagleout_female.geno_female.bgl.gz.phased\n";
	}
	open(FTFAM,">$dir/23/female/female.tfam");
	open(FTPED,">$dir/23/female/female.tped");
	$female = <FBUFF>;
	@female = split(" ",$female);
	$female = <FBUFF>;
	for($i=2;$i<@female;$i++)
	{
        	chomp($female[$i]);
        	if(exists($samp_hash{$female[$i]}))
        	{
                	print FTFAM "$samp_hash{$female[$i]}\n";
        	}
        	else
        	{
                	die "male tfam sample name not same as all tfam name\n";
        	}
        	$i++;
	}
	close(FTAM);
	open RSID, "$dir/23/female/markers.txt" or die "no file available $dir/23/female/markers.txt\n";
	while($line=<RSID>)
	{
        	chomp($line);
        	@marker = split(" ",$line);
        	$rsid{$marker[0]} = $marker[1];
	}
	close(RSID);

	while($line = <FBUFF>)
	{
        	chomp($line);
        	@female = split(" ",$line);
        	if(exists($rsid{$female[1]}))
        	{
                	$pos = $rsid{$female[1]};
        	}
        	else
		{
                	die "doesn't exist position for $female[1]";
        	}
        	print FTPED "23 $female[1] 0 $rsid{$female[1]} ";
        	for($i=2;$i<@female;$i++)
        	{
                	print FTPED " $female[$i]";
                	$i++;
                	print FTPED " $female[$i]";
        	}
        	print FTPED "\n";
	}
	close(FBUFF);
	close(FTPED);
}
